﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MicropleSoft.WmsApp.Library.ApplicationObserver;
using MicropleSoft.WmsApp.Library.ClientProxy;
using MicropleSoft.WmsApp.Library.Common;
using MicropleSoft.WmsApp.Library.LookUpInterface;
using MicropleSoft.WmsApp.Library.Proxy;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.WmsApp.Library.ApplicationCache
{
    public partial class BaseDataProvider : IBaseDataProvider
    {
        #region Implementation of IBaseDataProvider

        public BaseDataWrap GetRealTimeBaseDataByKey(string dataSourceKey)
        {
            LocalBaseDataWrapBuilderFactory.BuildLocalBaseData();

            IDataCache dataCache = CacheManage.GetDataCache();
            var data = dataCache.GetData(dataSourceKey);

            if(data !=null)
            {
                var baseDataWrap = (BaseDataWrap) data;
                if(baseDataWrap.PBaseDataWrapType == BaseDataWrapType.Server)
                {
                    RefreshBaseDataByKey(dataSourceKey);
                    data = dataCache.GetData(dataSourceKey);
                }
            }

            //通知所有的对象，修改数据源
            var list = DataSourceControlObserverFactory.CreateDataSourceControlObserver().GetControlsByDataSourceKey(dataSourceKey);
            foreach (var control in list)
            {
                if (control is IDataSource)
                {
                    var dataSource = (IDataSource)control;
                    if (dataSource.DataSourceUseCache)
                    {
                        dataSource.SetDataSource(((BaseDataWrap)data).BaseDataSource);
                    }
                }
            }
            return (BaseDataWrap)data;
        }

        public BaseDataWrap GetBaseDataByKey(string dataSourceKey)
        {
            LocalBaseDataWrapBuilderFactory.BuildLocalBaseData();

            IDataCache dataCache = CacheManage.GetDataCache();
            var data = dataCache.GetData(dataSourceKey);

            if (data != null)
            {
                //return (BaseDataWrap)data;
            }
            else
            {
                RefreshBaseDataByKey(dataSourceKey);
            }

            data = dataCache.GetData(dataSourceKey);

            //通知所有的对象，修改数据源
            var list = DataSourceControlObserverFactory.CreateDataSourceControlObserver().GetControlsByDataSourceKey(dataSourceKey);
            foreach (var control in list)
            {
                if(control is IDataSource)
                {
                    var dataSource = (IDataSource)control;
                    if(dataSource.DataSourceUseCache)
                    {
                        dataSource.SetDataSource(((BaseDataWrap)data).BaseDataSource);
                    }
                }
            }
            return (BaseDataWrap)data;
        }

        public void RefreshBaseDataByKey(string dataSourceKey)
        {
            var proxy = ServerProxyFactory.GetServerProxy();
            var data = (BaseDataWrap)proxy.CallService("GetBaseData", dataSourceKey, Security.UserSession.CurrentWhId, " and 1=1 ");
            IDataCache dataCache = CacheManage.GetDataCache();
            dataCache.AddData(dataSourceKey, data);
        }

        #endregion
    }
}
